package client.core;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import entity.Reply;
import entity.Request;

public class SyncTransfer {
	
	private int serverPort;
	private String serverAddress;
	
	public SyncTransfer(String serverAddress, int serverPort) {
		this.serverPort = serverPort;
		this.serverAddress = serverAddress;
	}

	public Reply transfer(Request req) {
		
		try {
			Socket ss = new Socket(serverAddress, serverPort);
			
			ObjectOutputStream oos = new ObjectOutputStream(ss.getOutputStream());
			ObjectInputStream ois = new ObjectInputStream(ss.getInputStream());
			
			oos.writeObject(req);

			Reply rep = (Reply) ois.readObject();

			ss.close();

			return rep;
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return null;
		
	}

}
